// pages/home/home.js

var utils = require('../../utils/utils.js')
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        oauthUserInfo: null,
        isOauth: false,
        userInfo: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.showUserInfoData();
        this.showUserInfo();
    },
    //数据库用户信息
    showUserInfo:function(){
        var user = wx.getStorageSync('userInfo');
        if(user){
            this.setData({
                userInfo:user
            })
        }else{
            this.setData({
                userInfo:{
                    credit:6888,
                    id:1008,
                    cash:2330.88
                }
            })
            wx.setStorageSync('userInfo', this.data.userInfo)
        }
    },
    //获取用户信息
    showUserInfoData: function() {
        if (this.data.oauthUserInfo) {
            return;
        }
        console.log(11);
        let _this = this;
        // 查看是否授权
        wx.getSetting({
            success: function(res) {
                console.log(res);
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                    wx.getUserInfo({
                        success: function(res) {
                            console.log(res.userInfo)
                            _this.setData({
                                isOauth: true,
                                oauthUserInfo: res.userInfo,
                            })
                            wx: wx.setStorage({
                                key: 'oauthUser',
                                data: res.userInfo
                            })
                            
                        }
                    })
                } else {
                    _this.setData({
                        isOauth: false
                    })
                }
            }
        })
    },
    //授权信息
    bindGetUserInfo: function(e) {

        if (!this.data.oauthUserInfo) {
            console.log(e);
            this.setData({
                oauthUserInfo: e.detail.userInfo
            })
            wx: wx.setStorage({
                key: 'oauthUser',
                data: e.detail.userInfo
            })
        }
    },
    blueTooth: function() {
        wx.navigateTo({
            url: '/pages/blueToothList/blueToothList',
        })
    },
    myAddress: function() {
        wx.navigateTo({
            url: '/pages/myAddress/myAddress',
        })
    },
    toDetail: function() {
        wx.navigateTo({
            url: '/pages/classDetail/classDetail',
        })
    },
    toMyOrder:function(){
        wx.navigateTo({
            url: '/pages/home/myOrder/myOrder',
        })
    },
    toOrder: function(e) {
        const type = Number(e.currentTarget.dataset.type)
        wx.navigateTo({
            url: '/pages/payOrder/payOrder?type=${type}',
        })
    },
    //打开积分商城
    creditShop: function() {
        wx.navigateTo({
            url: '/pages/creditShop/creditShop',
        })
    },
    //打开我的信息页面
    myProfile: function() {
        wx.navigateTo({
            url: './myProfile/myProfile',
        })
    },
    goSignData: function() {
        wx.navigateTo({
            url: '/pages/signDate/signDate',
        })
    },

    openDrawer: function(e) {

        utils.openDrawer(e, this);

    },
    powerDrawer: function(e) {
        utils.showModel(this, e.currentTarget.dataset.statu);
    }
})